import 'package:shop/common/AppStorge.dart';
import 'package:shop/common/shop_material.dart';
import 'package:shop/commonWidget/myImage.dart';
import 'package:shop/routers/Router.dart';

class LoginIndex {
  ///登录入口 需要登录的时候直接调用 LoginIndex.login()
  static login() {
    Map loginMap = MyStorge.login;
    if (loginMap.containsKey("def") && loginMap["def"] != "") {
      route.pushNamed(MyRoutes.quickLogin);
    } else {
      route.pushNamed(MyRoutes.codeLogin);
    }
  }

  static Widget _widget;
  //用户协议
  static Widget xieYi() {
    _getData() async {
      Map results = await MyHttp()
          .post(MyUrl.memberprotocol, needToken: false, cache: true);
      return Container(
          child: Row(
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[
          Text("登录或注册即同意",
              style: TextStyle(fontSize: 20.s, color: Color(0xFF999999))),
          InkWell(
            onTap: () {
              route.pushNamed(MyRoutes.myWeb,
                  arguments: results["MemberParams"]['Url']);
            },
            child: Text("《用户服务协议》",
                style: TextStyle(fontSize: 20.s, color: Color(0xFFFF6262))),
          ),
          Text("和", style: TextStyle(fontSize: 20.s, color: Color(0xFF999999))),
          InkWell(
            onTap: () {
              route.pushNamed(MyRoutes.myWeb,
                  arguments: results["PrivacyParams"]['Url']);
            },
            child: Text("《隐私政策》",
                style: TextStyle(fontSize: 20.s, color: Color(0xFFFF6262))),
          ),
        ],
      ));
    }

    if (_widget == null) {
      _widget = FutureBuilder(
        future: _getData(),
        builder: (BuildContext context, AsyncSnapshot<Widget> snapshot) {
          return Container(child: snapshot.data);
        },
      );
    }

    return _widget;
  }

  ///获取用户头像
  static Widget getHeader({String url = ""}) {
    if (url != "") {
      return Container(
          height: 128.h,
          width: 128.h,
          child: CircleAvatar(child: MyImage.network(url)));
    }
    Map loginMap = MyStorge.login;
    if (loginMap.containsKey("def") &&
        loginMap["def"] != "" &&
        loginMap[loginMap["def"]]["img"] != "") {
      return Container(
          height: 128.h,
          width: 128.h,
          child: CircleAvatar(
              child: MyImage.network(loginMap[loginMap["def"]]["img"])));
    }
    return Container(
      height: 128.h,
      width: 128.h,
      child: CircleAvatar(child: Image.asset("images/user/default head.png")),
    );
  }
}
